home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * Uuencode -- encode a file so that it's printable ascii, short lines
- *
- * Slightly modified from a version posted to net.sources a while back,
- * and suitable for compilation on the IBM PC
- *
- * modified for Lattice C on the ST - 11.05.85 by MSD
- * modified for ALCYON on the ST - 10-24-86 by RDR
- * modified a little more for MWC... 02/09/87 by JPHD
- * (An optional first argument of the form: -nnumber (e.g. -500), will
- * produce a serie of files that long, linked by the include statement,
- * such files are automatically uudecoded by the companion program.)
- * (A lot more to do about I/O speed, avoiding completely the stdio.h...)
- *
- */
-
-
- #include <stdio.h>
- #include <ctype.h>
-
- #define USAGE "Usage: uuencode [-n] inputfile\n"
-
- /* ENC is the basic 1 character encoding function to make a char printing */
- #define ENC(c) (((c) & 077) + ' ')
-
- extern FILE *freopen(), *fopen();
- FILE *fp;
- char ofname[80];
- extern char *rindex();
- int part = 'a';
- int split = 0; fileln = 32000;
-
- main(argc, argv)
- int argc; char *argv[];
- {
- if (argc < 2) {
- fprintf(stderr, USAGE);
- exit(2);
- }
- if (argv[1][0] == '-') {
- fileln = -atoi(argv[1]);
- if (fileln <= 0) {
- fprintf(stderr, "Wrong file length arg.\n");
- exit();
- }
- split = 1;
- argv++;
- }
- makename(argv[1]);
- if ((fp=fopen(argv[1], "rb"))==NULL) { /* binary input !!! */
- fprintf(stderr,"Cannot open %s\n",argv[1]);
- exit(1);
- }
- if(freopen(ofname, "w", stdout)!=stdout) {
- fprintf(stderr,"Cannot reassign stdout\n");
- exit(1);
- }
- maketable();
- printf("begin %o %s\n", 0644, argv[1]);
- encode();
- printf("end\n");
- exit(0);
- }
-
- /* create ASCII table so a mailer can screw it up and the decode
- * program can restore the error.
- */
- maketable()
- {
- register int i, j;
-
- puts("table");
- for(i = ' ', j = 0; i < '`' ; j++) {
- if (j == 32)
- putchar('\n');
- fputc(i++,stdout);
- }
- putchar('\n');
- }
-
- /* I include this in all of my programs to take the guess work out of
- * filenames.
- */
- makename(name)
- char name[];
- {
- register char *ptr;
-
- strcpy(ofname, name);
- /* I think index is neat; just look for a character, and
- * bomb it. Voila, you have a substring, no mess.
- */
- if(ptr = rindex(ofname,'.')) {
- *ptr = '\0';
- *--ptr = part;
- }
- else /* i.e. make the last character in the first name = part */
- ofname[strlen(ofname)-1] = part;
- strcat(ofname,".uue");
- return;
- }
-
- /*
- * copy from stdin to stdout, encoding as you go along.
- */
- encode()
- {
- char buf[80];
- char file[80];
- register int i, n;
- register int lines;
- lines = 6;
-
- strcpy(file,ofname);
- for (;;) {
- n = fr(buf, 45);
- putchar(ENC(n));
- for (i = 0; i < n; i += 3)
- outdec(&buf[i]);
- putchar(part);
- putchar('\n');
- ++lines;
- if (split && (lines > fileln)) {
- ++part;
- makename(file);
- printf("include %s\n",ofname);
- if(freopen(ofname, "w", stdout)!=stdout) {
- fprintf(stderr,"Cannot reassign stdout\n");
- exit(1);
- }
- maketable();
- printf("begin part %c\n",part);
- lines = 6;
- }
- if (n <= 0)
- break;
- }
- }
-
- /*
- * output one group of 3 bytes, pointed at by p, on file f.
- */
- outdec(p)
- register char *p;
- {
- register int c1, c2, c3, c4;
-
- c1 = *p >> 2;
- c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
- c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
- c4 = p[2] & 077;
- putchar(ENC(c1));
- putchar(ENC(c2));
- putchar(ENC(c3));
- putchar(ENC(c4));
- }
-
- /* fr: like read but stdio */
- int fr(buf, cnt)
- register char *buf;
- register int cnt;
- {
- register int c, i;
- for (i = 0; i < cnt; i++) {
- c = fgetc(fp);
- if (feof(fp))
- return(i);
- buf[i] = c;
- }
- return (cnt);
- }
-